package reflect;

import reflect.annotations.AutoRunClass;
import reflect.annotations.AutoRunMethod;

import java.lang.reflect.Method;
import java.util.Scanner;

public class Text {//reflect.Person
    public static void main(String[] args) throws Exception{
        System.out.println("请输入类名");
        Scanner sc = new Scanner(System.in);
        String className = sc.nextLine();
        Class cls = Class.forName(className);
        if(cls.isAnnotationPresent(AutoRunClass.class)){
            Object obj = cls.newInstance();//实例化对象
            Method[] methods = cls.getDeclaredMethods();//调用所有方法
            for(Method method : methods){//新循环遍历
                if(method.isAnnotationPresent(AutoRunMethod.class)){//判断方法是否被注解
                    int nam;
                    nam = method.getAnnotation(AutoRunMethod.class).time();
                    System.out.println("调用方法"+method.getName()+"()"+nam+"次");
                    for(int i = 0;i<nam;i++){
                        method.invoke(obj);//获取对象方法调用
                    }
                }

            }
        }else{
            System.out.println("该类没有被调用");
        }
    }
}
